package com.example.controller;

import com.example.common.Result;
import com.example.entity.Account;
import com.example.entity.Apply;
import com.example.entity.Contest;
import com.example.mapper.ApplyMapper;
import com.example.service.ApplyService;
import com.example.service.ContestService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.common.enums.ResultCodeEnum.APPLY_ALREDAY_ERROR;
import static com.example.common.enums.ResultCodeEnum.CONTEST_NOT_START;
import static com.example.utils.TokenUtils.getCurrentUser;

@RestController
@RequestMapping("/apply")
public class ApplyController {

    @Resource
    private ApplyService applyService;
    @Resource
    private ContestService contestService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Apply apply) {
        //先查询是否报名过
        Integer contestId = apply.getContestId();
        Account currentUser = getCurrentUser();
        Integer userId = currentUser.getId();

        Apply apply1 = new Apply();
        apply1.setContestId(contestId);
        apply1.setStudentId(userId);
        List<Apply> applies = applyService.selectAll(apply1);
        if (applies != null && !applies.isEmpty()) {
            return Result.error(APPLY_ALREDAY_ERROR);
        }
        //查询竞赛是否是报名中
        Integer status=contestService.getStatusById(contestId);
        if (status == 0){
            return Result.error(CONTEST_NOT_START);
        }

        applyService.add(apply);
        return Result.success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        applyService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        applyService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Apply apply) {
        System.out.println(apply);
        applyService.updateById(apply);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Apply apply = applyService.selectById(id);
        return Result.success(apply);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(Apply apply ) {
        List<Apply> list = applyService.selectAll(apply);
        return Result.success(list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(Apply apply,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Apply> page = applyService.selectPage(apply, pageNum, pageSize);
        return Result.success(page);
    }

    @GetMapping("/applycountC")
    public Result getC(){
        Map<String,Object> resultMap = new HashMap<>();
        Map<String, Integer> applyCountMap = new HashMap<>();
        List<Apply> list = applyService.selectAll(new Apply());
        for (Apply apply : list) {
            Integer contestid = apply.getContestId();
            // 这里可以对时间进行处理，例如打印、存储或其他操作
            String contest = contestService.selectById(contestid).getName();

            if (applyCountMap .containsKey(contest)) {
                // 如果月份已在 Map 中，将计数加 1
                applyCountMap .put(contest, applyCountMap .get(contest) + 1);
            } else {
                // 如果月份不在 Map 中，添加该月份并将计数初始化为 1
                applyCountMap .put(contest, 1);
            }
        }
        System.out.println(applyCountMap);
        resultMap.put("text","竞赛队伍数量统计");
        resultMap.put("subtext","统计维度：队伍数量");
        resultMap.put("applycount",applyCountMap);
        return Result.success(resultMap);
    }
}